﻿using System;
using System.Collections.Generic;
using System.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using KPIS.DBM;
using KPIS.GERP.GFMS.MODEL;
using KPIS.GERP.GFMS.IDAL;
using System.Data;
using System.Configuration;

namespace KPIS.GERP.GFMS.DAL
{
    public class D_ICM_MasContactBank : IDAL_ICM_MasContactBank
    {
        DBManager dbManager = new DBManager(SystemInfo._DataProvider, ConfigurationSettings.AppSettings["ConnectionString"].ToString());
        ConvertNullable cn = new ConvertNullable();

        public IList<M_ICM_MasContactBank> GetList(int? ConId, int? emp_seq = null)
        {
            string strSQL;
            DataRowCollection drc = null;
            IList<M_ICM_MasContactBank> infoList = new List<M_ICM_MasContactBank>();

            try
            {
                dbManager.Open();
                dbManager.CreateParameters(3);

                dbManager.AddParameters(0, "@delete_status", SystemInfo._DeleteRecordStatus);
                dbManager.AddParameters(1, "@ConId", ConId);
                dbManager.AddParameters(2, "@emp_seq", emp_seq);

                //if (!emp_seq.HasValue)
                //{
                    strSQL = strSQL = "SELECT acc.ConBankId as bank_acc_seq "
                                      +
                                      ", concat(acc.AccountCode, ' : ', acc.AccountName, '  ', b.bank_name) as bank_acc_name"
                                      + ", acc.AccountCode "
                                      + ", acc.AccountName "
                                      + ", bb.branch_seq "
                                      + ", bb.branch_id "
                                      + ", bb.branch_name "
                                      + ", b.bank_seq "
                                      + ", b.bank_id "
                                      + ", b.bank_name "
                                      + " FROM IcmMasContactBank acc"
                        //+ " LEFT JOIN fim_mas_bank_account_type_groups at ON acc.bank_acc_type_grp_seq = at.bank_acc_type_grp_seq"
                                      + " LEFT JOIN fim_mas_bank_branchs bb ON acc.BranchId = bb.branch_seq"
                                      + " LEFT JOIN fim_mas_banks b ON bb.bank_seq = b.bank_seq"
                        //+ " LEFT JOIN bgm_mas_source s ON acc.bgm_source_seq = s.bgm_source_seq"
                                      + " WHERE acc.RecordStatus not like @delete_status"
                                      + (ConId != null
                                          ? " and acc.ConId = @ConId"
                                          : "")
                        //+ (bgm_source_seq != null ?
                        //" and acc.bgm_source_seq = @bgm_source_seq" : "")
                                      + " ORDER BY bank_acc_name";
//                }
//                else
//                {
//                    strSQL =
//                        @"
//select 
//    eb.book_seq bank_acc_seq, 
//    concat(coalesce(eb.bank_acc_no,''), ' : ', e.first_name, ' ', e.last_name, '   ', coalesce(mb.bank_name, '')) as bank_acc_name
//, eb.bank_acc_no acc.AccountCode  
//, e.first_name, ' ', e.last_name as acc.AccountName  
//, bb.branch_seq  
//, bb.branch_id  
//, bb.branch_name  
//, b.bank_seq  
//, b.bank_id  
//, b.bank_name  
//from hrm_ps_employees e   
//    left join hrm_ps_emp_book_banks eb on e.emp_seq = eb.emp_seq
//    left join fim_mas_banks mb on mb.bank_seq = eb.bank_seq
//    LEFT JOIN fim_mas_bank_branchs bb ON mb.branch_seq = bb.branch_seq 
//    LEFT JOIN fim_mas_banks b ON bb.bank_seq = b.bank_seq 
//where eb.emp_seq = @emp_seq;";
//                }

                drc = dbManager.ExecuteDataSet(CommandType.Text, strSQL).Tables[0].Rows;
                if (drc.Count > 0)
                {
                    for (int i = 0; i < drc.Count; i++)
                    {
                        M_ICM_MasContactBank info = new M_ICM_MasContactBank();
                        info.Bank_Acc_Seq = Convert.ToInt32(drc[i]["bank_acc_seq"]);
                        info.Bank_Acc_Name = drc[i]["bank_acc_name"].ToString();

                        info.AccountCode = Convert.ToString(drc[i]["AccountCode"]);
                        info.AccountName = Convert.ToString(drc[i]["AccountName"]);

                        info.BranchId = Convert.ToInt32(drc[i]["branch_seq"]);
                        info.BranchCode = Convert.ToString(drc[i]["branch_id"]);
                        info.BranchName = Convert.ToString(drc[i]["branch_name"]);

                        info.BankId = Convert.ToInt32(drc[i]["bank_seq"]);
                        info.BankCode = Convert.ToString(drc[i]["bank_id"]);
                        info.BankName = Convert.ToString(drc[i]["bank_name"]);
                        
                        infoList.Add(info);
                    }
                }
            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                dbManager.Dispose();
            }

            return infoList;
        }
    }
}
